﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Restaurant.CoreType.Enums;
using Sector7.Restaurant.Modules.SYST.Utility;
using Sector7.Restaurant.Modules.SYST.LocalService;
using Sector7.Framework.Enums;
using Sector7.Framework.Utility;
using Sector7.Framework.Core;

namespace Sector7.Restaurant.Modules.SYST.Windows
{
    /// <summary>
    /// AddMetaDataWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddMetadataWindow : AddModeWindowBase
    {
        public AddMetadataWindow(RS_MetadataType selectedType, List<RS_MetadataType> types)
        {
            InitializeComponent();

            cbbTypes.ItemsSource = types;
            cbbTypes.SelectedItem = selectedType;
        }

        public RS_Metadata Metadata
        {
            get { return m_entity as RS_Metadata; }
            set { m_entity = value; }
        }

        protected override void InitializeForCreating()
        {
            m_entity = new RS_Metadata();

            RS_Metadata metadata = m_entity as RS_Metadata;

            metadata.IsValid = true;
            metadata.TypeId = (int)cbbTypes.SelectedValue;
        }

        protected override void InitializeForUpdating()
        {
            btnContinue.Visibility = Visibility.Hidden;
        }

        /// <summary>
        /// 添加元数据
        /// </summary>
        /// <returns></returns>
        protected override void AddData()
        {
            switch (m_crud)
            {
                case CrudType.Create:
                    Metadata.Id = Aide.LocalClient.Invoke<int>(
                        service => service.AddMetadata(Metadata));
                    break;
                case CrudType.Update:
                    EntityDelta delta = ClientUtil.CreateDelta<EntityDelta>(typeof(RS_Metadata));
                    delta.Conditions.Add("Id", Metadata.Id);
                    delta.DeltaProperties.Add("Name", Metadata.Name);
                    delta.DeltaProperties.Add("Keyword", Metadata.Keyword);
                    delta.DeltaProperties.Add("TypeId", Metadata.TypeId);

                    Aide.LocalClient.Invoke<int>(
                        service => service.SetMetadata(delta));
                    break;
                default:
                    break;
            }

            base.AddData();
        }
    }
}
